﻿using System;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using T2.Logging.ASPMVC.Controllers;
using T2.Logging.UI.MVC.Controllers;
using T2.Logging.ASPMVC.Entity;
using T2.Logging.ASPMVC.Entity.Interface;
using Microsoft.QualityTools.Testing.Fakes;
using System.Net.Http;
using T2.Logging.ASPMVC.Entity.Search;


namespace T2.WebSecurity.UI.MVC.Tests.Controllers
{
    [TestClass]
    public class LogCategoryController
    {
        private static List<ILogCategory_ui> retVal;

        [TestMethod]
        public void LogCategory_1_Add()
        {
            string testUserName = "trooks";
            string testPassword = "duminda123";
            bool testRememberMe = false;


            LogCategoryAPIController controller = new LogCategoryAPIController();
            LogCategory_ui param = new LogCategory_ui();
            param.Action = "Add";

            param.Role = "user";

            using (ShimsContext.Create())
            {
                System.Web.Security.Fakes.ShimMembership.GetUser = () =>
                {
                    return new System.Web.Security.MembershipUser(
                        "T2MembershipProvider", "trooks", 1, "dumindapium@gmail.com", "car",
                        "none", true, false, Convert.ToDateTime("1/1/2013"), Convert.ToDateTime("1/1/2013"),
                        Convert.ToDateTime("1/10/2013"), Convert.ToDateTime("1/1/2013"), Convert.ToDateTime("1/1/2013"));
                };
                System.Web.Security.Fakes.ShimFormsAuthentication.SetAuthCookieStringBoolean = (userName, rememberMe) =>
                {
                    Assert.AreEqual(testUserName, userName);
                    Assert.AreEqual(testRememberMe, rememberMe);
                };
                System.Web.Security.Fakes.ShimRoles.GetRolesForUser = () =>
                {
                    return new string[] { "super admin" };
                };
                WebMatrix.WebData.WebSecurity.Login(testUserName, testPassword, false);


                HttpResponseMessage msg = controller.PostLogCategory(param);
                Assert.IsNotNull(msg);
                Assert.IsTrue(msg.StatusCode == System.Net.HttpStatusCode.OK || msg.StatusCode == System.Net.HttpStatusCode.NotAcceptable);
            }

        }

        [TestMethod]
        public void LogCategory_2_GetAll()
        {
            string testUserName = "trooks";
            string testPassword = "duminda123";
            bool testRememberMe = false;


            LogCategoryAPIController controller = new LogCategoryAPIController();
            LogCategory_ui param = new LogCategory_ui();
            param.Action = "GetAll";
            param.PageNumber = 1;
            param.PageSize = 100;
            param.Role = "user";
            param.SortColumn = "LogCategoryID";
            param.SortType = "asc";

            using (ShimsContext.Create())
            {
                System.Web.Security.Fakes.ShimMembership.GetUser = () =>
                {
                    return new System.Web.Security.MembershipUser(
                        "T2MembershipProvider", "trooks", 1, "dumindapium@gmail.com", "car",
                        "none", true, false, Convert.ToDateTime("1/1/2013"), Convert.ToDateTime("1/1/2013"),
                        Convert.ToDateTime("1/10/2013"), Convert.ToDateTime("1/1/2013"), Convert.ToDateTime("1/1/2013"));
                };
                System.Web.Security.Fakes.ShimFormsAuthentication.SetAuthCookieStringBoolean = (userName, rememberMe) =>
                {
                    Assert.AreEqual(testUserName, userName);
                    Assert.AreEqual(testRememberMe, rememberMe);
                };
                System.Web.Security.Fakes.ShimRoles.GetRolesForUser = () =>
                {
                    return new string[] { "super admin" };
                };
                WebMatrix.WebData.WebSecurity.Login(testUserName, testPassword, false);


                retVal = controller.GetLogCategory(param.Action,"trooks",param.SortColumn,param.SortType,param.Role,param.PageNumber, param.PageSize);
                Assert.IsNotNull(retVal);
                Assert.IsTrue(retVal[0].LogCategoryID > 0);
            }
            
        }


        [TestMethod]
        public void LogCategory_4_Search()
        {
            string testUserName = "trooks";
            string testPassword = "duminda123";
            bool testRememberMe = false;


            LogCategoryAPIController controller = new LogCategoryAPIController();
            LogCategorySearch_ui param = new LogCategorySearch_ui();
            param.Action = "Search";
            param.PageNumber = 1;
            param.PageSize = 100;
            param.Role = "user";
            param.SortColumn = "LogCategoryID";
            param.SortType = "asc";
            

            using (ShimsContext.Create())
            {
                System.Web.Security.Fakes.ShimMembership.GetUser = () =>
                {
                    return new System.Web.Security.MembershipUser(
                        "T2MembershipProvider", "trooks", 1, "dumindapium@gmail.com", "car",
                        "none", true, false, Convert.ToDateTime("1/1/2013"), Convert.ToDateTime("1/1/2013"),
                        Convert.ToDateTime("1/10/2013"), Convert.ToDateTime("1/1/2013"), Convert.ToDateTime("1/1/2013"));
                };
                System.Web.Security.Fakes.ShimFormsAuthentication.SetAuthCookieStringBoolean = (userName, rememberMe) =>
                {
                    Assert.AreEqual(testUserName, userName);
                    Assert.AreEqual(testRememberMe, rememberMe);
                };
                System.Web.Security.Fakes.ShimRoles.GetRolesForUser = () =>
                {
                    return new string[] { "super admin" };
                };
                WebMatrix.WebData.WebSecurity.Login(testUserName, testPassword, false);


                retVal = controller.GetLogCategory(param.Action, "trooks", param.SortColumn, param.SortType, param.Role, param.PageNumber, param.PageSize);
                Assert.IsNotNull(retVal);
                Assert.IsTrue(retVal[0].LogCategoryID > 0);
            }

        }

        

        [TestMethod]
        public void LogCategory_3_Edit()
        {
            string testUserName = "trooks";
            string testPassword = "duminda123";
            bool testRememberMe = false;


            LogCategoryAPIController controller = new LogCategoryAPIController();
            LogCategory_ui param = null;
            param = (retVal.Count > 1) ? (LogCategory_ui)retVal[1] : new LogCategory_ui();
            param.Action = "Update";
            param.LogCategoryName = "TheTest2";
            param.Role = "user";

            using (ShimsContext.Create())
            {
                System.Web.Security.Fakes.ShimMembership.GetUser = () =>
                {
                    return new System.Web.Security.MembershipUser(
                        "T2MembershipProvider", "trooks", 1, "dumindapium@gmail.com", "car",
                        "none", true, false, Convert.ToDateTime("1/1/2013"), Convert.ToDateTime("1/1/2013"),
                        Convert.ToDateTime("1/10/2013"), Convert.ToDateTime("1/1/2013"), Convert.ToDateTime("1/1/2013"));
                };
                System.Web.Security.Fakes.ShimFormsAuthentication.SetAuthCookieStringBoolean = (userName, rememberMe) =>
                {
                    Assert.AreEqual(testUserName, userName);
                    Assert.AreEqual(testRememberMe, rememberMe);
                };
                System.Web.Security.Fakes.ShimRoles.GetRolesForUser = () =>
                {
                    return new string[] { "super admin" };
                };
                WebMatrix.WebData.WebSecurity.Login(testUserName, testPassword, false);


                HttpResponseMessage msg = controller.PostLogCategory(param);
                Assert.IsNotNull(msg);
                Assert.IsTrue(msg.StatusCode == System.Net.HttpStatusCode.OK || msg.StatusCode == System.Net.HttpStatusCode.NotAcceptable);
            }

        }

        [TestMethod]
        public void LogCategory_4_Delete()
        {
            string testUserName = "trooks";
            string testPassword = "duminda123";
            bool testRememberMe = false;


            LogCategoryAPIController controller = new LogCategoryAPIController();
            LogCategory_ui param = new LogCategory_ui();
            param = (retVal.Count > 1) ? (LogCategory_ui)retVal[1] : new LogCategory_ui();
            param.Action = "Remove";
            param.Role = "user";

            using (ShimsContext.Create())
            {
                System.Web.Security.Fakes.ShimMembership.GetUser = () =>
                {
                    return new System.Web.Security.MembershipUser(
                        "T2MembershipProvider", "trooks", 1, "dumindapium@gmail.com", "car",
                        "none", true, false, Convert.ToDateTime("1/1/2013"), Convert.ToDateTime("1/1/2013"),
                        Convert.ToDateTime("1/10/2013"), Convert.ToDateTime("1/1/2013"), Convert.ToDateTime("1/1/2013"));
                };
                System.Web.Security.Fakes.ShimFormsAuthentication.SetAuthCookieStringBoolean = (userName, rememberMe) =>
                {
                    Assert.AreEqual(testUserName, userName);
                    Assert.AreEqual(testRememberMe, rememberMe);
                };
                System.Web.Security.Fakes.ShimRoles.GetRolesForUser = () =>
                {
                    return new string[] { "super admin" };
                };
                WebMatrix.WebData.WebSecurity.Login(testUserName, testPassword, false);


                HttpResponseMessage msg = controller.PostLogCategory(param);
                Assert.IsNotNull(msg);
                Assert.IsTrue(msg.StatusCode == System.Net.HttpStatusCode.OK || msg.StatusCode == System.Net.HttpStatusCode.NotAcceptable);
            }

        }
    }
}
